package org.traffometer.traffosim;

import java.util.Observable;


/**
 * 
 * This class implements Observable so that the GUI View class can observe and react to any state
 * changes in this model. The notifyChange method implements this observable behavior and sends
 * the view a String representation of the state of the simulation.
 * @author Joshua Stupplebeen
 *
 */
public class TraffosimModel extends Observable {
  
  private static TraffosimModel theInstance = new TraffosimModel();
  
  /**
   * Default constructor.
   *
   */
  private TraffosimModel() {
    //for PMD
  }
  
  

  /**
   * Returns an instance of the TraffosimModel.
   * @return Instance of the TraffosimModel.
   */
  public static synchronized TraffosimModel getInstance() {
    return theInstance;
  }
  
  /**
   * Implements the run button functionality of the GUI.
   * @param controller Controller in charge of packet manipulation.
   *
   */
  public synchronized void runSim(Controller controller) {
    try {
      notifyChange("Run event generated");
      notifyChange("Sending Packets:" + controller.printPackets());
      controller.sendPackets();
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  /**
   * Implements the browse button functionality of the GUI.
   *
   */
  public synchronized void browse() {
    notifyChange("Browse functionality not implemented.");
  }

  /**
   * Implements the clear button functionality of the GUI.
   *
   */
  public synchronized void clearOutput() {
    notifyChange("");
  }
  
  
  /**
   * Informs any observers of this TraffosimModel that its state has changed.
   * @param state Identifies the output to the gui's textfield.
   */
  private void notifyChange(String state) {
    setChanged();
    notifyObservers(state);
  }

}
